package com.hzh.cusviewdemo.Adapter;

import android.content.Context;
import android.view.View;

import com.hzh.cusviewdemo.Bean.MusicSearchData;
import com.hzh.cusviewdemo.Bean.MyMusicData;
import com.hzh.cusviewdemo.R;
import com.hzh.cusviewdemo.Util.DialogUtils;

import java.util.List;

/**
 * 歌单歌曲适配器
 */
public class SongMenuMusicAdapter extends BaseLoadMoreDataAdapter<MyMusicData> {

    public OnListenMusicListener mListener;

    public interface OnListenMusicListener {
        void onListenMusic(MyMusicData musicData, int pos);

        void onLongClickMusic(MyMusicData musicData, int pos);
    }

    public void setOnListenMusicListener(OnListenMusicListener listener) {
        this.mListener = listener;
    }

    public SongMenuMusicAdapter(Context context, List<MyMusicData> dataList) {
        super(context, dataList);
    }

    @Override
    public void bindData(BaseRecyclerViewHolder holder, int position, MyMusicData item) {
        holder.setText(R.id.imm_no, (position + 1) + "");
        holder.setText(R.id.imm_song_name, item.getName());
        holder.setText(R.id.imm_song_owner, item.getSonger());
        holder.setOnClick(R.id.imm_linear_music, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyMusicData musicData = getData().get(position);
                if (musicData == null) {
                    DialogUtils.showErrorToast("当前歌曲异常，无法收听~");
                    return;
                }
                if (musicData.getSid().isEmpty()) {
                    DialogUtils.showErrorToast("当前歌曲异常，无法收听~");
                    return;
                }
                if (mListener == null) {
                    DialogUtils.showErrorToast("操作异常，请重新进入本页面播放~");
                    return;
                }
                mListener.onListenMusic(musicData, position);
            }
        });
        holder.setOnLongClick(R.id.imm_linear_music, new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                MyMusicData musicData = getData().get(position);
                if (musicData != null && mListener != null) {
                    mListener.onLongClickMusic(musicData, position);
                }
                return true; // 不会长按后同时触发点击事件
            }
        });
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_menu_music;
    }

}
